/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReplyHuman;
import com.boxin.ims.modules.wechat.dao.QuestionnaireReplyHumanDao;

/**
 * 调查问卷回答的用户Service
 * @author XiaoQian
 * @version 2014-05-10
 */
@Component
@Transactional(readOnly = true)
public class QuestionnaireReplyHumanService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(QuestionnaireReplyHumanService.class);
	
	@Autowired
	private QuestionnaireReplyHumanDao questionnaireReplyHumanDao;
	
	public QuestionnaireReplyHuman get(Long id) {
		return questionnaireReplyHumanDao.findOne(id);
	}
	
	public Page<QuestionnaireReplyHuman> find(Page<QuestionnaireReplyHuman> page, QuestionnaireReplyHuman questionnaireReplyHuman) {
		DetachedCriteria dc = questionnaireReplyHumanDao.createDetachedCriteria();
		dc.createAlias("questionnaire", "questionnaire");
		if (StringUtils.isNotEmpty(questionnaireReplyHuman.getName())){
			dc.add(Restrictions.like("name", "%"+questionnaireReplyHuman.getName()+"%"));
		}
		if (questionnaireReplyHuman.getQuestionnaire()!=null && questionnaireReplyHuman.getQuestionnaire().getId()>0){
			dc.add(Restrictions.eq("questionnaire.id", questionnaireReplyHuman.getQuestionnaire().getId()));
		}
		dc.addOrder(Order.desc("id"));
		return questionnaireReplyHumanDao.find(page, dc);
	}
	
	public List<QuestionnaireReplyHuman> getByWechatId(Long memberId,Long questionnaireId){
		return questionnaireReplyHumanDao.getByWechatId(memberId, questionnaireId);
	}
	
	@Transactional(readOnly = false)
	public void save(QuestionnaireReplyHuman questionnaireReplyHuman) {
		questionnaireReplyHumanDao.save(questionnaireReplyHuman);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		questionnaireReplyHumanDao.deleteById(id);
	}
	
}
